package com.example.kermit.zhpaper.ui.activity;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.kermit.zhpaper.R;
import com.example.kermit.zhpaper.config.MyAppConfig;
import com.example.kermit.zhpaper.ui.Fragment.LatestFragment;
import com.example.kermit.zhpaper.ui.Fragment.ContentFragment;
import com.example.kermit.zhpaper.ui.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;


public class MainActivity extends BaseActivity implements LatestFragment.LatestFragmentListener, ContentFragment.ContentFragmentListener{

    private static final String LOG_TAG = "MainActivity";

    @Bind(R.id.id_toolbar)
    Toolbar toolbar;
    @Bind(R.id.id_drawerlayout)
    DrawerLayout drawerLayout;
    @Bind(R.id.id_navigation)
    NavigationView navigationView;

    ActionBarDrawerToggle drawerToggle;

    LatestFragment latestFragment;
    ContentFragment contentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        latestFragment = new LatestFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.id_main_layout, latestFragment);
        transaction.commit();


        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawertoggle_open,
                R.string.drawertoggle_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };

        drawerToggle.syncState();
        drawerLayout.setDrawerListener(drawerToggle);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.nav_home:
                        break;
                    case R.id.nav_name:
                        break;
                    case R.id.nav_sex:
                        break;
                    case R.id.nav_age:
                        break;
                }
                menuItem.setChecked(true);//点击了把它设为选中状态
                drawerLayout.closeDrawers();//关闭抽屉
                return true;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void LatestFragmentCallback(int newId) {
        contentFragment = new ContentFragment();
        String newsUrl = MyAppConfig.INFO_CONTENT_WITHOUT_ID + newId;
        Bundle bundle = new Bundle();
        bundle.putString("newsUrl", newsUrl);
        contentFragment.setArguments(bundle);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(latestFragment);
        transaction.add(R.id.id_main_layout, contentFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void contentFragmentCallback() {

    }
}
